# 编写分组表业务
from rest_framework.viewsets import ModelViewSet
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from django.contrib.auth.models import Group, Permission

from apps.meiduo_admin.serializers.groups import GroupSerialzier
from apps.meiduo_admin.utils import NumPage
from apps.meiduo_admin.serializers.perms import PermissionSerialzier


class GroupsView(ModelViewSet):
    # 指定序列化器
    serializer_class = GroupSerialzier
    # 指定查询集
    queryset = Group.objects.all()
    # 指定权限
    permission_classes = [IsAdminUser]
    # 指定分页
    pagination_class = NumPage

    # 获取简单权限数据
    def simple(self, request):
        perms = Permission.objects.all()
        ser = PermissionSerialzier(perms, many=True)
        return Response(ser.data)
